

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import View.*;

/**
 * Servlet implementation class AdminLoginServlet
 */
@WebServlet("/admin/loginAdmin")
public class AdminLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = (String) request.getParameter("username");
		String password = (String) request.getParameter("password");
		UserView user = (UserView) request.getSession().getAttribute("user");
		System.out.println(username + " " + password);
		String url = "/admin/loginAdmin.jsp";
		if (user == null) {
			if (username != null && password != null)
				user = Controller.login(username, password);
		}
		if (user != null) {
			if (user.getRank() == 0) {
				url = "/admin/admin.jsp";
			} else if (user.getRank() == 1) {
				url = "/index.jsp";
			}
			request.getSession().setAttribute("user", user);
		}
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
		dispatcher.forward(request, response);
	}

}
